package com.yelobblog.yelobblog.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yelobblog.yelobblog.common.exception.ApiException;
import com.yelobblog.yelobblog.common.exception.ErrorEnum;
import com.yelobblog.yelobblog.common.untils.ThreadLocalUtil;
import com.yelobblog.yelobblog.common.untils.userStatusEnum;
import com.yelobblog.yelobblog.domain.entity.logs;
import com.yelobblog.yelobblog.mapper.logsMapper;
import com.yelobblog.yelobblog.service.logsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class logsServiceimpl extends ServiceImpl<logsMapper, logs> implements logsService {
    @Resource
    private logsMapper logsMapper;

    @Override
    public Page<logs> getLogs(Integer pageNum, Integer pageSize){
        Page<logs> page = new Page<>(pageNum,pageSize);
        return logsMapper.getLogs(page);
    }
    @Override
    public void addLogs(String title, String content) {
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        Integer role = (Integer) claims.get("role");
        if (!role.equals(userStatusEnum.SUPERUSER.getCode())) {
            throw new ApiException(ErrorEnum.PERMISSION_DENIED.getErrorCode(), ErrorEnum.PERMISSION_DENIED.getErrorMsg());
        }
        logs logs = new logs();
        logs.setTitle(title);
        logs.setContent(content);
        logs.setUserId(userId);
        logsMapper.insert(logs);
    }
}
